package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.CoreLibrary;
import com.baomidou.ant.entity.ProcessCondition;
import com.baomidou.ant.entity.SysUser;
import com.baomidou.ant.entity.WorkOrder;
import com.baomidou.ant.mapper.ProcessConditionMapper;
import com.baomidou.ant.service.*;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.RequestAddMouldVo;
import com.baomidou.ant.vo.request.RequestConditionListVo;
import com.baomidou.ant.vo.response.ResponseConditionListVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ProcessConditionServiceImpl extends ServiceImpl<ProcessConditionMapper, ProcessCondition> implements IProcessConditionService {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ICoreLibraryService coreLibraryService;
    @Autowired
    private IPinLibraryService pinLibraryService;

    @Override
    public IPage<ResponseConditionListVo> listCondition(RequestConditionListVo vo) {
        IPage<ProcessCondition> page=new Page<>(vo.getDisplayStart(),vo.getDisplayLength());
        QueryWrapper<ProcessCondition> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(vo.getMouldName())){
            queryWrapper.eq("mould_name",vo.getMouldName());
        }
        if(StringUtils.isNotBlank(vo.getMouldNum())){
            queryWrapper.eq("mould_num",vo.getMouldNum());
        }
        SysUser user=userService.getById(vo.getUserId());
        //判断管理员级别
        if(Objects.equals(user.getType(), "1")){
            //管理员条件查询
            if(StringUtils.isNotBlank(vo.getDeptName())){
                Integer deptId=deptService.queryByDeptName(vo.getDeptName());
                queryWrapper.eq("dept_id",deptId);
            }
        }else{
            queryWrapper.eq("dept_id",user.getDeptId());
        }
        //得到结果集
        IPage<ProcessCondition> conditionIPage=page(page,queryWrapper);
        return conditionIPage.convert(processCondition -> {
            ResponseConditionListVo responseConditionListVo=new ResponseConditionListVo();
            BeanUtils.copyProperties(processCondition,responseConditionListVo);
            if(Objects.equals(processCondition.getCoreIds(), "0")){
                responseConditionListVo.setCoreName("全CORE适用");

            }else{
                int[] cores= MyStringUtil.toInt(processCondition.getCoreIds());
                String[] coreNames= new String[cores.length];
                for(int i=0;i<cores.length;i++){
                    coreNames[i]=coreLibraryService.getById(cores[i]).getCorePin();
                }
                responseConditionListVo.setCoreName(MyStringUtil.strToString(coreNames));
            }
            responseConditionListVo.setPinName(pinLibraryService.getById(processCondition.getPinId()).getPinName());
            responseConditionListVo.setDeptName(deptService.getById(processCondition.getDeptId()).getDept());
            return  responseConditionListVo;
        });
    }

    @Override
    public boolean addMould(RequestAddMouldVo vo) {
        ProcessCondition condition=new ProcessCondition();
        BeanUtils.copyProperties(vo,condition);
        //判断是否有模板
        if(list().size()==0){
            String mouldKey=MyStringUtil.getRandomStringArray(10,1)[0];
            condition.setMouldKey(mouldKey);
            String mouldNum=MyStringUtil.getPinYinHeadChar(deptService.getById(vo.getDeptId()).getDept())+"1";
            condition.setMouldNum(mouldNum);
        }else {
            //生成标识码
            String mouldKey=MyStringUtil.getRandomStringArray(10,1)[0];
            for(int i=0;i<list().size();i++){
                if(ObjectUtils.isNotEmpty(queryByKey(mouldKey))){
                    mouldKey=MyStringUtil.getRandomStringArray(10,1)[0];
                }else
                    break;
            }
            //生成编号
            ProcessCondition lastOne=list().get(list().size()-1);
            String mouldNum=MyStringUtil.getPinYinHeadChar(deptService.getById(vo.getDeptId()).getDept())+
                    (Integer.parseInt(lastOne.getMouldNum().replaceAll("[a-zA-Z]", ""))+1);
            condition.setMouldKey(mouldKey);
            condition.setMouldNum(mouldNum);
        }
        return save(condition);
    }

    @Override
    public ProcessCondition queryByKey(String mouldKey) {
        QueryWrapper<ProcessCondition> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mould_key",mouldKey);
        return getOne(queryWrapper);
    }



}
